/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Screen.java
 *
 * Created on Aug 15, 2009, 3:45:08 PM
 */

package embuzzled.ui;

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

/**
 *
 * @author Satyajeet
 */
public class Screen extends javax.swing.JFrame {

    GridIOManager giom;
    PlayerController MyPlayerController;
     GCell[][] GameGrid;
     GridSolution Screen_GridSolution;
     Grid ScreenGrid;
    int g_rows, g_cols;
    /** Creates new form Screen */
    public Screen() {
        initComponents();
        myInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
    	jSeparator1 = new javax.swing.JSeparator();
        ScreenGridPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ScreenGridlist = new javax.swing.JList();
        GridLoad = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        Playerlist = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        Play = new javax.swing.JButton();
        LaunchEditor = new javax.swing.JButton();
        puzzleCount = new javax.swing.JLabel();
        saveImage = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Embuzzled Game Simulator");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        ScreenGridPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout ScreenGridPanelLayout = new javax.swing.GroupLayout(ScreenGridPanel);
        ScreenGridPanel.setLayout(ScreenGridPanelLayout);
        ScreenGridPanelLayout.setHorizontalGroup(
            ScreenGridPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 887, Short.MAX_VALUE)
        );
        ScreenGridPanelLayout.setVerticalGroup(
            ScreenGridPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 571, Short.MAX_VALUE)
        );

        ScreenGridlist.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        ScreenGridlist.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(ScreenGridlist);

        GridLoad.setText("Load");
        GridLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GridLoadActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Select a grid");

        Playerlist.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        Playerlist.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(Playerlist);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("Select Player");

        Play.setText("Play");
        Play.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlayActionPerformed(evt);
            }
        });

        LaunchEditor.setText("Editor");
        LaunchEditor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LaunchEditorActionPerformed(evt);
            }
        });

        puzzleCount.setText("No. of puzzles");

        saveImage.setText("Save as Image");
        saveImage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveImageActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(LaunchEditor)
                        .addGap(49, 49, 49)
                        .addComponent(saveImage))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ScreenGridPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(puzzleCount, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Play)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(GridLoad)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LaunchEditor)
                    .addComponent(saveImage))
                .addGap(7, 7, 7)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Play)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(ScreenGridPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(GridLoad)
                    .addComponent(puzzleCount))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    	
    	
    }// </editor-fold>//GEN-END:initComponents

    private void LaunchEditorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LaunchEditorActionPerformed
        // TODO add your handling code here:

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditorScreen().setVisible(true);
            }
        });
    }//GEN-LAST:event_LaunchEditorActionPerformed

    private void GridLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GridLoadActionPerformed
        // TODO add your handling code here:


         String filename = (String)ScreenGridlist.getSelectedValue();
        if(filename == null)
        {
            return;
        }


         ScreenGrid = null;
        try {
            ScreenGrid = giom.readGridfromFile(filename);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
        }
             gridBuilder(ScreenGrid);



    }//GEN-LAST:event_GridLoadActionPerformed

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        // TODO add your handling code here:
        populateGridlist();
    }//GEN-LAST:event_formWindowGainedFocus

    private void PlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlayActionPerformed
        // TODO add your handling code here:

        // Get the player's name:
        String playername = (String)Playerlist.getSelectedValue();
        if(playername == null)
        {
            System.out.println("Select a player");
            return;

        }
        if(playername.equalsIgnoreCase(""))
        {
            return;
        }
        if(ScreenGrid == null)
        {
           System.out.println("Select a screen");
            return;
            
        }

        try {
            Screen_GridSolution = MyPlayerController.makePlay(ScreenGrid, playername);
            solutionBuilder(Screen_GridSolution);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_PlayActionPerformed

    private void saveImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveImageActionPerformed
        // TODO add your handling code here:

         String filename = JOptionPane.showInputDialog(null, "Enter Filename");
         String filenamex = "./Output_Images/"+filename+".png";
         String filenamey = "./Output_Images/"+filename+"_alt.png";

        int width = ScreenGridPanel.getWidth();
        int height = ScreenGridPanel.getHeight();
       
        BufferedImage myImage = new BufferedImage(width+80, height+80, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = myImage.createGraphics();
        ScreenGridPanel.paint(g);
        g.setStroke(new BasicStroke(3));
        g.setColor(Color.white);
        g.fillRect(0, height,width+80, 80);
        g.fillRect(width, 0,80,height+ 80);
        
        g.setColor(Color.black);
        g.drawOval(width, height, 50, 50);
        Font font = new Font("Arial", Font.PLAIN, 22);
        g.setFont(font);
        
        g.drawString(Integer.toString(Screen_GridSolution.no_of_puzzles),	width+10 , height+30);
        //g.drawString(Integer.toString(12),	width+10 , height+30);
        g.dispose();
        File imageFile = new File(filenamex);
        try {
            ImageIO.write(myImage, "png", imageFile);
        } catch (IOException ex) {
            Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
        }
        String filepath = imageFile.getAbsolutePath();
        writeSolutionKey(filename);
        // Now let us also save the solution key
        
        
        //System.out.println(filepath);
        // This part commented out for MAC OS systems
        
//        String command = "explorer.exe " + filepath;
//        try {
//            Runtime.getRuntime().exec(command);
//        } catch (Exception ef) {
//            System.out.println("Could not open");
//
//        }

        // Making the alternate image
        AltCanvas altcanvas = new AltCanvas(Screen_GridSolution,ScreenGrid);
        int total_width = 25 * Screen_GridSolution.col;
        int total_height = 25 * Screen_GridSolution.row;
        width = total_width;
        height = total_height;
        
        BufferedImage myImage2 = new BufferedImage(total_width+80, total_height+80, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = myImage2.createGraphics();
        altcanvas.paint(g2);
        g2.setStroke(new BasicStroke(3));
        g2.setColor(Color.white);
        g2.fillRect(0, height,width+80, 80);
        g2.fillRect(width, 0,80,height+ 80);
        
        g2.setColor(Color.black);
        g2.drawOval(width, height, 50, 50);
        Font font2 = new Font("Arial", Font.PLAIN, 22);
        g2.setFont(font2);
        
        g2.drawString(Integer.toString(Screen_GridSolution.no_of_puzzles),	width+10 , height+30);
        //g.drawString(Integer.toString(12),	width+10 , height+30);
        g2.dispose();
        File imageFile2 = new File(filenamey);
        try {
            ImageIO.write(myImage2, "png", imageFile2);
        } catch (IOException ex) {
            Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        


    }//GEN-LAST:event_saveImageActionPerformed


    public void writeSolutionKey(String filename)
    {
    	String filenamesk = "./Output_Images/"+filename+".txt";
    	
    	try{
    	FileWriter outFile = new FileWriter(filenamesk);
    	
    	PrintWriter out = new PrintWriter(outFile);
    	out.println(Screen_GridSolution.SolutionKey);
    	out.close();
    	}catch(Exception e)
    	{
    		System.out.println("Could not save solution file.");
    	}
    }
    
     public void populateGridlist()
    {

          ScreenGridlist.removeAll();
          
         // Let us also populate the list with the gridnames

         ArrayList<String> gridnames = giom.getnames();
         ScreenGridlist.setModel(new DefaultListModel());
         DefaultListModel dlm = (DefaultListModel) ScreenGridlist.getModel();
         for(int loop=0;loop<gridnames.size();loop++)
         {
             dlm.addElement((Object)gridnames.get(loop));
         }
    }

    public void populatePlayerlist()
    {
        Playerlist.removeAll();

        ArrayList<String> Pnames = MyPlayerController.getPlayerList();
        Playerlist.setModel(new DefaultListModel());
        DefaultListModel dlm = (DefaultListModel) Playerlist.getModel();
         for(int loop=0;loop<Pnames.size();loop++)
         {
             dlm.addElement((Object)Pnames.get(loop));
             
         }

    }

    public void solutionBuilder(GridSolution gs)
    {
    	initComponents2();
        int rows = gs.row;
        int cols = gs.col;
        g_rows = rows;
        g_cols = cols;
        // The logical end of grid
        GameGrid = null;
        GameGrid = new GCell[rows][cols];

        ScreenGridPanel.removeAll();

         ScreenGridPanel.setLayout(new GridLayout(rows, cols));


        float gridheight = ScreenGridPanel.getHeight();
        float gridwidth = ScreenGridPanel.getWidth();

        // Now let us calculate size for block
        int blockheight = (int)(gridheight/rows);
        int blockwidth = (int)(gridwidth/cols);
        
        if(blockheight<= blockwidth)
        {
            blockwidth = blockheight;
        }
        else
        {
            blockheight = blockwidth;
        }

        for (int loopr = 0; loopr< rows; loopr++)
        {
            for(int loopc = 0; loopc<cols; loopc++)
            {

                JPanel jp = new JPanel();
                Point pp = new Point(loopr, loopc);
                GCell gc = new GCell(jp, pp);
                gc.setPreferredSize(new Dimension(blockwidth ,blockheight));
                gc.setMaximumSize(new Dimension(blockwidth,blockheight));
                //gc.setBorder(BorderFactory.createLineBorder(Color.GRAY));
                //gc.setBorder(BorderFactory.createEtchedBorder());


                gc.setBackground(gs.GridColors[loopr][loopc]);

                if(ScreenGrid.datagrid[loopr][loopc] == true)
                {
                    gc.setBackground(Color.DARK_GRAY);
                }

                gc.setToolTipText(loopr + "," + loopc);

                GameGrid[loopr][loopc] = gc;



                ScreenGridPanel.add(gc);


            }
            //System.out.println("Done!");
             ScreenGridPanel.repaint();
             ScreenGridPanel.validate();
        }

        puzzleCount.setText("Number of puzzles = " + gs.no_of_puzzles);


    }


     public void gridBuilder(Grid mygrid)
    {
    	 initComponents2();
//         int rows = Integer.parseInt(EditorRows.getText());
//        int cols = Integer.parseInt(EditorCols.getText());

        int rows = mygrid.rows;
        int cols = mygrid.cols;

        g_rows = rows;
        g_cols = cols;
        // The logical end of grid
        GameGrid = null;
        GameGrid = new GCell[rows][cols];

        ScreenGridPanel.removeAll();

         ScreenGridPanel.setLayout(new GridLayout(rows, cols));


        float gridheight = ScreenGridPanel.getHeight();
        float gridwidth = ScreenGridPanel.getWidth();

        // Now let us calculate size for block
        int blockheight = (int)(gridheight/rows);
        int blockwidth = (int)(gridwidth/cols);
        
        if(blockheight<= blockwidth)
        {
            blockwidth = blockheight;
        }
        else
        {
            blockheight = blockwidth;
        }
        

        for (int loopr = 0; loopr< rows; loopr++)
        {
            for(int loopc = 0; loopc<cols; loopc++)
            {

                JPanel jp = new JPanel();
                Point pp = new Point(loopr, loopc);
                GCell gc = new GCell(jp, pp);
                gc.setPreferredSize(new Dimension(blockwidth ,blockheight));
                gc.setMaximumSize(new Dimension(blockwidth,blockheight));
                //gc.setBorder(BorderFactory.createEtchedBorder());
                gc.setBorder(BorderFactory.createLineBorder(Color.GRAY));
               
                gc.GBool = mygrid.datagrid[loopr][loopc];
                if(gc.GBool == true)
                {
                    gc.setBackground(Color.DARK_GRAY);
                }
                gc.setToolTipText(loopr + "," + loopc);

                GameGrid[loopr][loopc] = gc;



                ScreenGridPanel.add(gc);


            }
            //System.out.println("Done!");
             ScreenGridPanel.repaint();
             ScreenGridPanel.validate();
        }


    }

     
         public void initComponents2()
         {
               setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Embuzzled Game Simulator");
            addWindowFocusListener(new java.awt.event.WindowFocusListener() {
                public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                    formWindowGainedFocus(evt);
                }
                public void windowLostFocus(java.awt.event.WindowEvent evt) {
                }
            });

            ScreenGridPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

            javax.swing.GroupLayout ScreenGridPanelLayout = new javax.swing.GroupLayout(ScreenGridPanel);
            ScreenGridPanel.setLayout(ScreenGridPanelLayout);
            ScreenGridPanelLayout.setHorizontalGroup(
                ScreenGridPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 887, Short.MAX_VALUE)
            );
            ScreenGridPanelLayout.setVerticalGroup(
                ScreenGridPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 571, Short.MAX_VALUE)
            );

            
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(LaunchEditor)
                            .addGap(49, 49, 49)
                            .addComponent(saveImage))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(ScreenGridPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(puzzleCount, javax.swing.GroupLayout.Alignment.LEADING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(Play)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(GridLoad)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)))))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(LaunchEditor)
                        .addComponent(saveImage))
                    .addGap(7, 7, 7)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(6, 6, 6)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(Play)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(ScreenGridPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(GridLoad)
                        .addComponent(puzzleCount))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );

            pack();

    	 
     }
     

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Screen().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton GridLoad;
    private javax.swing.JButton LaunchEditor;
    private javax.swing.JButton Play;
    private javax.swing.JList Playerlist;
    private javax.swing.JPanel ScreenGridPanel;
    private javax.swing.JList ScreenGridlist;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel puzzleCount;
    private javax.swing.JButton saveImage;
    // End of variables declaration//GEN-END:variables

    private void myInit() {
        //throw new UnsupportedOperationException("Not yet implemented");

        giom = new GridIOManager();
        MyPlayerController = new PlayerController();
        populateGridlist();
        populatePlayerlist();

    }

}


class AltCanvas extends Canvas{

	GridSolution gs_local;
	Grid sg_local;
	public AltCanvas(GridSolution gs, Grid ScreenGrid)
	{
		gs_local = gs;
		sg_local = ScreenGrid;
	}
	
	public void paint(Graphics GraphicsObject)
	{
		int width = 25;
		int height = 25;
		int rows = gs_local.row;
		int cols = gs_local.col;
		
		
		for(int loopc = 0;loopc<cols;loopc++)
		{
			for(int loopr=0;loopr<rows;loopr++)
			{
				int x = loopc * width;
				int y = loopr * height;
				GraphicsObject.setColor(gs_local.GridColors[loopr][loopc]);
				if(sg_local.datagrid[loopr][loopc] == true)
				{
					GraphicsObject.setColor(Color.white);
				}
				GraphicsObject.fillRect(x, y, width, height);
				
				
			}
			
		}
		
	}

}

